package com.txd.mofa;

import android.app.Application;
import android.content.Context;

import com.bytedance.sdk.open.aweme.TikTokOpenApiFactory;
import com.bytedance.sdk.open.aweme.TikTokOpenConfig;
import com.danikula.videocache.HttpProxyCacheServer;
import com.kongzue.baseokhttp.util.BaseOkHttp;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.txd.mofa.net.NetStateReceiver;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import cn.jpush.android.api.JPushInterface;
import me.jessyan.autosize.AutoSizeConfig;

import static com.txd.mofa.xiangce.models.album.AlbumModel.instance;

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();


        CrashHandler.getInstance().init(getApplicationContext());

        /*开启网络广播监听*/
        NetStateReceiver.registerNetworkStateReceiver(getApplicationContext());

        //极光
        JPushInterface.setDebugMode(BuildConfig.DEBUG);
        JPushInterface.init(this);

        //友盟 初始化SDK
        UMConfigure.init(this, UMConfigure.DEVICE_TYPE_PHONE,"");
        // 选用AUTO页面采集模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);

        //抖音
        String clientkey = "awyrrvspqke05vhm"; // 需要到开发者网站申请
        TikTokOpenApiFactory.init(new TikTokOpenConfig(clientkey));

        //以下是屏幕适配代码
        AutoSizeConfig.getInstance().setUseDeviceSize(true).setCustomFragment(true);


        BaseOkHttp.DEBUGMODE = true;
        BaseOkHttp.TIME_OUT_DURATION = 30;
        BaseOkHttp.serviceUrl = "https://magic.magichairai.com/index.php/Api/";
    }

    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy(Context context) {
        MyApplication app = (MyApplication) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer(this);
    }

    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.white, android.R.color.darker_gray);//全局设置主题颜色
                return new MaterialHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }
}
